//生成菜单
var menuItem = Vue.extend({
	name: 'menu-item',
	props:{item:{},index:0},
	template:[
	          '<li :class="{active: (item.type===0 && index === 0)}">',
				  '<a v-if="item.type === 0" href="javascript:;">',
					  '<i v-if="item.icon != null" :class="item.icon"></i>',
					  '<span>{{item.name}}</span>',
					  '<i class="fa fa-angle-left pull-right"></i>',
				  '</a>',
				  '<ul v-if="item.type === 0" class="treeview-menu">',
					  '<menu-item :item="item" :index="index" v-for="(item, index) in item.list"></menu-item>',
				  '</ul>',
				  '<a v-if="item.type === 1" :href="\'#\'+item.url">' +
					  '<i v-if="item.icon != null" :class="item.icon"></i>' +
					  '<i v-else class="fa fa-circle-o"></i> {{item.name}}' +
				  '</a>',
	          '</li>'
	].join('')
});
//校验密码
function isPasswd(s) {
    //至少8-16个字符，至少1个大写字母，1个小写字母和1个数字，其他可以是任意字符
    var patrn = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/;
    if (!patrn.exec(s)) return false
    return true
}
//iframe自适应
$(window).on('resize', function() {
	var $content = $('.content');
	$content.height($(this).height() - 120);
	$content.find('iframe').each(function() {
		$(this).height($content.height());
	});
}).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);

window.indexVm = new Vue({
	el:'#rrapp',
	data:{
		msgs:"",
		user:{},
		menuList:{},
		main:"",	// iframe 的 src
		password:'',
		newPassword:'',
        navTitle:"首页",
        msgshow:null
	},
	methods: {
		pel:function(){
			  this.contains_lovercase = /[a-z]/.test(this.password);
		      this.contains_number = /\d/.test(this.password);
		      this.contains_uppercase = /[A-Z]/.test(this.password);
		      
		},
		getMenuList: function () {
			$.getJSON(baseURL + "sys/menu/nav", function(r){
                indexVm.menuList = r.menuList;
				console.log(r.menuList);
                window.permissions = r.permissions;
			});
		},
		getUser: function(){
			$.getJSON(baseURL + "sys/user/info", function(r){
				console.log(r);
				if(r != null && r.msg != null){
					indexVm.msgs="("+r.msg+")";
				}
                indexVm.user = r.user;
				// window.user = r.user;
                $.getJSON(baseURL + "sys/user/info/" + r.user.userId, function(r){
                    // console.log(r);
					window.user = r.user;

					var pWindow = window.parent;
					var url = window.location.hash;
					console.log(url);
					console.log(url.replace('#',''));
					if (!url.replace('#', '') || url.replace('#', '') == 'main.html') {
                        indexVm.main = 'main.html';
						var oIframe = $('#main-content iframe', pWindow.document);
						// console.log(oIframe);
						oIframe.attr('src',indexVm.main);
					} else {
                        indexVm.main = url.replace('#', '');
						var oIframe = $('#main-content iframe', pWindow.document);
						oIframe.attr('src',indexVm.main);
					}
                });
			});
		},
		updatePassword: function(){

			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "修改密码",
				area: ['550px', '270px'],
				shadeClose: false,
				content: jQuery("#passwordLayer"),
				btn: ['修改','取消'],
				btn1: function (index) {
					var data = "password="+indexVm.password+"&newPassword="+indexVm.newPassword;
                    console.log(indexVm.newPassword);
                    if ('' == indexVm.newPassword || null == indexVm.newPassword) {
                        alert("密码不能为空！");
                        return;
                    }
                    if (!isPasswd(indexVm.newPassword)) {
                        alert("密码格式不正确！");
                        return;
                    }
                    var key = CryptoJS.enc.Utf8.parse("o7H8uIM2O5qv65l2");
                    var src = CryptoJS.enc.Utf8.parse(data);
                    var encrypted = CryptoJS.AES.encrypt(src, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
					$.ajax({
						type: "POST",
					    url: baseURL + "sys/user/password",
					    data: "user="+encrypted.toString(),
					    dataType: "json",
					    success: function(r){
							if(r.code == 0){
								layer.close(index);
								layer.alert('修改成功', function(){
									location.reload();
								});
							}else{
								layer.alert(r.msg);
							}
						}
					});
	            }
			});
		},
        logout: function () {
			//删除本地token
            localStorage.removeItem("token");
            //跳转到登录页面
            location.href = baseURL + 'login.html';
        },
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        }
	},
	created: function(){
        this.getMenuList();
        this.getUser();
	},
    updated: function(){
		// console.log('路由');
		// console.log('indexVm' + indexVm.main);
        //路由
        var router = new Router();
        routerList(router, indexVm.menuList);
        router.start();
    }
});



function routerList(router, menuList){
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.type == 0){
			routerList(router, menu.list);
		}else if(menu.type == 1){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
                indexVm.main = url.replace('#', '');
			    
			    //导航菜单展开
			    $(".treeview-menu li").removeClass("active");
                $(".sidebar-menu li").removeClass("active");
			    $("a[href='"+url+"']").parents("li").addClass("active");

                indexVm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}
